package cn.bluemobi.dylan.step.presenter.my.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import cn.bluemobi.dylan.step.base.RxPresenter;
import cn.bluemobi.dylan.step.moder.bean.Goods;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.moder.http.CommonSubscriber;
import cn.bluemobi.dylan.step.moder.http.RetrofitHelper;
import cn.bluemobi.dylan.step.moder.http.RxUtil;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import cn.bluemobi.dylan.step.moder.http.response.PageDataBean;
import cn.bluemobi.dylan.step.moder.prefs.Prefs;
import cn.bluemobi.dylan.step.moder.prefs.SharedPreferencesHelper;
import cn.bluemobi.dylan.step.presenter.contract.my.StepcountContract;
import cn.bluemobi.dylan.step.moder.bean.StepData;
import cn.bluemobi.dylan.step.step.utils.DbUtils;
import cn.bluemobi.dylan.step.utils.common.DateUtil;
import cn.bluemobi.dylan.step.utils.common.LogUtils;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by $Liudan on 2018/8/7 0007.
 */
public class StepcountPresenter extends RxPresenter<StepcountContract.View> implements StepcountContract.Presenter {
    @Inject
    SharedPreferencesHelper prefHelper;
    private int currPage = 1;
    private int pageSize = 20;



    @Inject
    public StepcountPresenter() {

    }

    /**
     * 获取所有历史数据
     */
    public void dbGetStepcountList(){


        Observable<List<StepData>> observable = Observable.create(new ObservableOnSubscribe<List<StepData>>() {
            @Override
            public void subscribe(ObservableEmitter<List<StepData>> emitter) throws Exception {
                List<StepData> list = DbUtils.getQueryAll(StepData.class);
                emitter.onNext(list);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<List<StepData>> observer = new Observer<List<StepData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(List<StepData> programList) {
                if (programList != null && programList.size() > 0) {
                    mView.showContent(programList);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }

    /**
     * 获取当天数据
     */
    public void daGetTodyStepcount(){
        Observable<List<StepData>> observable = Observable.create(new ObservableOnSubscribe<List<StepData>>() {
            @Override
            public void subscribe(ObservableEmitter<List<StepData>> emitter) throws Exception {
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String sdfDate =  sdf.format(date); //当前日期
                long CURRENT_DATE =DateUtil.dateToStampDay(sdfDate);
                //获取当天的数据，用于展示
                List<StepData> list = DbUtils.getQueryByWhere(StepData.class, "stepTime", new Long[]{CURRENT_DATE});
                emitter.onNext(list);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<List<StepData>> observer = new Observer<List<StepData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(List<StepData> result) {
                int setpcount = 0;
                if (result.size() == 0 || result.isEmpty()) {
                    setpcount =  0;
                } else if (result.size() == 1) {
                    LogUtils.debug(getClass().getName(),"1111111111111111StepData==="+result.get(0).toString());
                    setpcount =  result.get(0).getStepNumber();
                } else {
                    LogUtils.debug(getClass().getName(),"1111111111111111出错了！");
                }
                mView.showTodayStepcount(setpcount);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }


    /**
     * 获取user
     */
    public void preUser() {
        Observable<User> observable = Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> emitter) throws Exception {
                User user = prefHelper.getPrefObject(Prefs.key_user, User.class);
                emitter.onNext(user);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<User> observer = new Observer<User>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(User user) {
                if (user != null) {
                    mView.showUserInfo(user);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }

    List<StepData> stepList = new ArrayList<>();
    /**
     * 服务器步数记录
     */
    public void httpStepList(int memberId,boolean isRefresh) {
        if(isRefresh){
            currPage = 1;
        }
        addSubscribe(RetrofitHelper.getStepcountApi().memberStepList(currPage, pageSize, memberId,2)
                .compose(RxUtil.<HttpResponseBean<PageDataBean<List<StepData>>>>rxSchedulerHelper())
                .compose(RxUtil.<PageDataBean<List<StepData>>>handleResult())
                .subscribeWith(new CommonSubscriber<PageDataBean<List<StepData>>>(mView) {
                    @Override
                    public void onNext(PageDataBean<List<StepData>> result) {
                        currPage = result.getCurrent();
                        if(currPage==1){ //刷新
                            stepList = result.getRecords();
                        }else {
                            stepList.addAll(result.getRecords());
                        }
                        if(mView!=null){
                            mView.httpStepList(stepList);
                        }
                        currPage++;
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        if(mView!=null){
                            mView.closeDialog();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(mView!=null){
                            mView.closeDialog();
                        }
                    }
                })
        );
    }

}
